<template>
    <div class="login-container">
        <el-form :model="ruleForm" :rules="rules"
                 status-icon
                 ref="ruleForm"
                 label-position="left"
                 label-width="0px"
                 class="demo-ruleForm login-page">
            <h3 class="title">注册</h3>
            <el-form-item prop="username">
                <el-input type="text"
                          v-model="ruleForm.username"
                          placeholder="用户名"
                ></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input type="password"
                          v-model="ruleForm.password"
                          placeholder="密码"
                ></el-input>
            </el-form-item>
            <el-form-item prop="name">
                <el-input type="text"
                          v-model="ruleForm.name"
                          placeholder="姓名"
                ></el-input>
            </el-form-item>
            <el-form-item>
                <el-radio-group v-model="ruleForm.sex">
                    <el-radio-button label="男"></el-radio-button>
                    <el-radio-button label="女"></el-radio-button>
                </el-radio-group>
                <el-date-picker
                    style="float:right;"
                    v-model="ruleForm.date"
                    type="date"
                    placeholder="出生日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item prop="phone">
                <el-input type="text"
                          v-model="ruleForm.phone"
                          placeholder="电话"
                ></el-input>
            </el-form-item>
            <el-form-item prop="email">
                <el-input type="text"
                          v-model="ruleForm.email"
                          placeholder="邮箱"
                ></el-input>
            </el-form-item>
            <el-form-item align="center">
                <el-radio v-model="type" label="resume">求职人员</el-radio>
                 <el-radio style="position: relative;left: 10px" v-model="type" label="companyuser">招聘人员</el-radio>
            </el-form-item>
            <el-form-item style="width:100%;">
                <el-button type="primary" style="width:100%;" @click="handleSubmit">注册</el-button>
            </el-form-item>
        </el-form>
    </div>
    
</template>

<script>
    export default {
        name: "Register",
        data(){
            return{
                ruleForm: {
                    username: '',
                    password: '',
                    name:'',
                    sex:'男',
                    phone:'',
                    email:'',
                    date:'',
                },
                //绑定默认
                type: 'resume',
                //非空校验
                rules: {
                    username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
                    password: [{required: true, message: '请输入密码', trigger: 'blur'}]
                }
            }
        },
        //后台请求
        methods: {
            handleSubmit(){
               // console.log(this.ruleForm)
               // console.log(this.type)
                this.$refs.ruleForm.validate((valid) => {
                    if(valid){
                        let _this = this

                        if(_this.type == 'resume'){
                            axios.get('http://localhost:8182/resume/register', {params:_this.ruleForm}).then(function (resp) {
                                if(resp.data.code == -1){
                                    _this.$alert('用户名已经存在', '提示', {
                                        confirmButtonText: '确定'
                                    })
                                }
                                if(resp.data.code == 0){
                                    //跳转到登录界面
                                    _this.$alert('注册成功', '提示', {
                                        confirmButtonText: '确定'
                                    })
                                    _this.$router.replace({path: '/'})//跳转页面
                                }
                            })
                        }

                        if(_this.type == 'companyuser'){
                            axios.get('http://localhost:8182/companyuser/register', {params:_this.ruleForm}).then(function (resp) {
                                if(resp.data.code == -1){
                                    _this.$alert('用户名已经存在', '提示', {
                                        confirmButtonText: '确定'
                                    })
                                }
                                if(resp.data.code == 0){
                                    //跳转到登录界面
                                    _this.$alert('注册成功', '提示', {
                                                confirmButtonText: '确定'
                                    })
                                    _this.$router.replace({path: '/'})//跳转页面
                                }
                    })
                }
                    

                    }
                }) 
            }
        }
    };
</script>

<style scoped>
    .login-container {
        width: 100%;
        height: 100%;
    }
    .login-page {
        -webkit-border-radius: 5px;
        border-radius: 5px;
        margin: 50px auto;
        width: 350px;
        padding: 35px 35px 15px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }
    label.el-checkbox.rememberme {
        margin: 0px 0px 15px;
        text-align: left;
    }
</style>